#include "stdafx.h"
#include "RibbonButton.h"
#include "FCTabView.h"
#include "FCTabPage.h"

RibbonButton::RibbonButton(){
    m_arrowType = 0;
    setBackColor(FCColor_None);
    setBorderColor(FCColor_None);
}

RibbonButton::~RibbonButton(){
}

int RibbonButton::getArrowType(){
    return m_arrowType;
}

void RibbonButton::setArrowType(int arrowType){
    m_arrowType = arrowType;
}

bool RibbonButton::isSelected(){
    FCView *parent = getParent();
    if (parent){
        FCTabView *tabView = dynamic_cast<FCTabView*>(parent);
        if (tabView){
            FCTabPage *selectedTabPage = tabView->getSelectedTabPage();
            if (selectedTabPage){
                if (this == selectedTabPage->getHeaderButton()){
                    return true;
                }
            }
        }
    }
    return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Long RibbonButton::getPaintingTextColor(){
   if (isEnabled())
    {
        return FCColor::rgb(0, 0, 0);
    }
    else
    {
        return FCColor::rgb(150, 150, 150);
    }
}

void RibbonButton::onPaintBackground(FCPaint *paint, const FCRect& clipRect){
    FCNative *native = getNative();
    int width = getWidth(), height = getHeight();
    int mw = width / 2, mh = height / 2;
    FCRect drawRect = {0, 0, width, height};
    int cornerRadius = 0;
    bool isTabHeader = false;
    cornerRadius = 4;
    if (m_arrowType > 0){
        cornerRadius = 0;
    }
    FCView *parent = getParent();
    if (parent){
        FCTabView *tabView = dynamic_cast<FCTabView*>(parent);
        if (tabView){
            cornerRadius = 0;
            isTabHeader = true;
        }
    }
    if (isTabHeader)
    {
        drawRect.top = 1;
        drawRect.bottom -= 1;
        if (isSelected())
        {
            paint->fillRect(FCColor::rgb(200, 200, 200), drawRect);
        }
        else
        {
            paint->fillRect(FCColor::rgb(220, 220, 220), drawRect);
            paint->drawLine(FCColor::rgb(70, 76, 79), 1, 0, width - 3, height / 4, width - 3, height * 3 / 4);
        }
    }else{
        //paint->fillGradientRect(FCCOLORS_BACKCOLOR, FCCOLORS_BACKCOLOR2, drawRect, cornerRadius, m_angle);
        //paint->drawRoundRect(FCColor_Border, 1, 0, drawRect, cornerRadius);
        if (m_arrowType > 0){
            FCPoint point1 = {0};
            FCPoint point2 = {0};
            FCPoint point3 = {0};
            int ts = std::min(mw, mh) / 2;
            switch (m_arrowType){
                case 1:
                    point1.x = mw - ts;
                    point1.y = mh;
                    point2.x = mw + ts;
                    point2.y = mh - ts;
                    point3.x = mw + ts;
                    point3.y = mh + ts;
                    break;
                case 2:
                    point1.x = mw + ts;
                    point1.y = mh;
                    point2.x = mw - ts;
                    point2.y = mh - ts;
                    point3.x = mw - ts;
                    point3.y = mh + ts;
                    break;
                case 3:
                    point1.x = mw;
                    point1.y = mh - ts;
                    point2.x = mw - ts;
                    point2.y = mh + ts;
                    point3.x = mw + ts;
                    point3.y = mh + ts;
                    break;
                case 4:
                    point1.x = mw;
                    point1.y = mh + ts;
                    point2.x = mw - ts;
                    point2.y = mh - ts;
                    point3.x = mw + ts;
                    point3.y = mh - ts;
                    break;
            }
            FCPoint points[3];
            points[0] = point1;
            points[1] = point2;
            points[2] = point3;
            paint->fillPolygon(FCColor::rgb(0, 0, 0), points, 3);
        }
        bool state = false;
        if (this == native->getPushedView()){
            state = true;
            paint->fillRoundRect(FCColor::rgba(255, 255, 255, 25), drawRect, cornerRadius);
        }
        else if (this == native->getHoveredView()){
            state = true;
            paint->fillRoundRect(FCColor::rgba(0, 0, 0, 25), drawRect, cornerRadius);
        }
        if(state){
            if(cornerRadius > 0){
                paint->drawRoundRect(FCColor::rgb(255, 255, 255), 1, 0, drawRect, cornerRadius);
            }
            else{
                paint->drawRect(FCColor::rgb(255, 255, 255), 1, 0, drawRect);
            }
        }
    }
}
